package com.oocl.servlet;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.oocl.exception.MyException;
import com.oocl.pojo.Food;
import com.oocl.service.FoodService;
import com.oocl.service.impl.FoodServiceImpl;
import com.oocl.util.FileNameUtil;
import com.oocl.util.FileUtil;
import com.oocl.util.PropertiesFactory;

/**
 * Servlet implementation class UpdateServlet
 */
public class UpdateFoodServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateFoodServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		FoodService fSer = new FoodServiceImpl();
		DiskFileItemFactory factory=new DiskFileItemFactory(20*1024, new File("D:/temp/"));
		ServletFileUpload upload=new ServletFileUpload(factory);
		upload.setSizeMax(1000*1024);
		upload.setHeaderEncoding("UTF-8");//控制中文文件名乱码
		String icnum=(String)request.getSession().getAttribute("icnum");
		String fid=null;
		String fName=null;
		double price=0.0;
		String fType=null;
		String path=null;
		String fimage ="";
		try {
			List<FileItem> items = upload.parseRequest(request);
			for (FileItem item:items) {
				if (item.isFormField()) {
					if ("fid".equals(item.getFieldName())) {  
						fid=item.getString();
					}
					if ("fname".equals(item.getFieldName())) {  
						fName=item.getString();
					}
					if ("fprice".equals(item.getFieldName())) {
						price=Double.parseDouble(item.getString());
					}
					if ("ftype".equals(item.getFieldName())) {
						fType=item.getString();
					}
				}else {
					fimage = FileNameUtil.getFileName()+".png";
					path= PropertiesFactory.getPro().getProperty("fileroot")+PropertiesFactory.getPro().getProperty("foodpath")+File.separator+fimage;    //随机产生文件名,前面需要读取配置文件
				    System.out.println(path);
					FileUtil.toFile(item.getInputStream(), path);					
				}				
			}
		
		
		Food food = fSer.loadOne(fid);
		
		food.setFid(fid);
		food.setFimage(fimage);
		food.setFname(fName);
		food.setFprice(price);
		food.setFtype(fType);
		food.setIcnum(icnum);
		
	
			if (fSer.update(food)>0) {
				//添加成功返回页面，转发
				request.getRequestDispatcher("foodlist").forward(request, response);
			}else{
				response.sendRedirect("exception.jsp");
				return;
			}
		} catch (FileUploadException e) {
//			e.printStackTrace();
			throw new MyException("file not found");
		}catch (MyException e) {
			throw e;
		}
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
